<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/16
 * Time: 15:40
 */
namespace app\home\controller;
use app\models\Bases;
use think\Controller;
use think\Config;
class Index extends Base{
    /**
     * @return \think\response\View
     * 普通用户登录界面
     */
    public function index(){
        $information = new Information();
        $userInfo = self::getUserInfoByCookie();
        $announcement = $information->getAnnouncement();
        $typeInfo = $information->getType(); //获取栏目信息
        return view('index', [
                'userRand'=>substr((string)time(),-5).rand(100,999),
                'userInfo'=>$userInfo,
                'typeInfo'=>$typeInfo,
                'announcement' => $announcement
            ]);
    }

    /**
     * @return \think\response\View
     * 巡管登录界面
     */
    public function verify(){
        $information = new Information();
        $userInfo = self::getUserInfoByCookie();
        $announcement = $information->getAnnouncement();
        $typeInfo = $information->getType(); //获取栏目信息
        return view('verify',[
            'userRand'=>substr((string)time(),-5).rand(100,999),
            'userInfo'=>$userInfo,
            'typeInfo'=>$typeInfo,
            'announcement' => $announcement
        ]);
    }

    /**
     * @return \think\response\Json
     * 获取栏目的数据接口
     */
    public function typeData(){
        $information = new Information();
        $integal     = new Integral();
        $post = input('param.');
        $result = $information->getTypeData($post['type'],$post['column_id']);
        //根据不同的参数类型显示不同的模版
        if($post['type'] == $this->TYPE_CODE){
            return view('core',['result'=>$result]);
        }
        if($post['type'] == $this->TYPE_STOCK){
            $result = $information->everyStock($result);                //如果是每日牛股 则需要返回当前登录用户是否阅读
            $is_deduction_integral = $integal->checkDeductionIntegral();//判断用户是否有扣除积分
            $deduct_integral = $information->deductIntegral();          //计算该用户本次应该阅读扣除多少个积分
            return view('everyday',['result'=>$result,'is_deduction_integral'=>$is_deduction_integral,'integral'=>Config::get('integral.reading'),'deduct_integral'=>$deduct_integral]);
        }
        if($post['type'] == $this->TYPE_SPLENDID){
            return view('splendid',['result'=>$result]); //精彩回放
        }
        if($post['type'] == $this->TYPE_ADVERT){
            return view('decision',['result'=>$result]);
        }
    }

    /**
     * @return \think\response\Json
     * 获取核心内参一条数据
     */
    public function getCoreOne(){
        try{
            $article = new Bases('article');
            $article_id = input('param.article_id');
            $result = $article->find("id = $article_id");
            if(!$result){
                throw  new \LogicException('遗憾，数据丢失',10028);
            }
            //转换时间格式
            $result['create_time'] = date('Y-m-d',$result['create_time']);
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult($result));
    }

    /**
     * @return \think\response\Json
     * 获取一条牛股详情信息
     */
    public function getShareOne(){
        try{
            $share = new Bases('shares');
            $share_id = input('param.share_id');
            $result = $share->find("shares_id = $share_id");
            if(!$result){
                throw  new \LogicException('遗憾，数据丢失',10028);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult($result));
    }

    /**
     * 生成网页快捷方式
     */
    public function saveDesk(){
        $ShortCut = "[InternetShortcut]
        URL=http://zbstest.guochengic.com/home/index/index
        IDList=
        [{000214A0-0000-0000-C000-000000000046}]
        Prop3=19,2";
        $fname = iconv('utf-8','GBk',"国诚投顾");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename={$fname}.url;");
        echo $ShortCut;
    }
}